package pl.us.ij.plugins.segmentation.forms;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import ij.ImagePlus;
import pl.us.ij.plugins.segmentation.SegmentationPlugIn;
import pl.us.ij.plugins.segmentation.SwitchImageListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-04-13
 * Time: 19:29:13
 * To change this template use File | Settings | File Templates.
 */
public class ChannelForm implements SwitchImageListener {
    private JPanel panel1;
    private JCheckBox redCheckBox;
    private JCheckBox greenCheckBox;
    private JCheckBox blueCheckBox;
    private JButton allButton;

    public ChannelForm() {
        redCheckBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!(greenCheckBox.isSelected() || blueCheckBox.isSelected())) {
                    redCheckBox.setSelected(true);
                }

            }
        });
        greenCheckBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!(redCheckBox.isSelected() || blueCheckBox.isSelected())) {
                    greenCheckBox.setSelected(true);
                }

            }
        });
        blueCheckBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!(greenCheckBox.isSelected() || redCheckBox.isSelected())) {
                    blueCheckBox.setSelected(true);
                }

            }
        });

        allButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setRed(true);
                setGreen(true);
                setBlue(true);
            }
        });

        SegmentationPlugIn.addSwitchImageListener(this);
    }

    public boolean isRed() {
        return redCheckBox.isSelected();
    }

    public void setRed(boolean value) {
        redCheckBox.setSelected(value);
    }

    public boolean isGreen() {
        return greenCheckBox.isSelected();
    }

    public void setGreen(boolean value) {
        greenCheckBox.setSelected(value);
    }

    public boolean isBlue() {
        return blueCheckBox.isSelected();
    }

    public void setBlue(boolean value) {
        blueCheckBox.setSelected(value);
    }

    private void disableForm() {
        blueCheckBox.setEnabled(false);
        redCheckBox.setEnabled(false);
        greenCheckBox.setEnabled(false);
        panel1.setEnabled(false);
        panel1.updateUI();
    }

    private void enableForm() {
        blueCheckBox.setEnabled(true);
        redCheckBox.setEnabled(true);
        greenCheckBox.setEnabled(true);
        panel1.setEnabled(true);
        panel1.updateUI();
    }

    @Override
    public void imageActivated(ImagePlus imp) {
        ImagePlus iPlus = imp;

        if (iPlus.getProcessor().getNChannels() == 1) {
            disableForm();
        } else {
            enableForm();
        }
    }

    @Override
    public void imageDeactivated(ImagePlus deactivatedImp, ImagePlus imp) {
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:d:grow", "center:max(d;4px):noGrow,top:0dlu:noGrow,center:max(d;4px):noGrow"));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new FormLayout("center:d:grow", "center:d:noGrow"));
        panel2.setBackground(new Color(-1));
        CellConstraints cc = new CellConstraints();
        panel1.add(panel2, cc.xy(1, 1, CellConstraints.DEFAULT, CellConstraints.FILL));
        panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), null));
        final JLabel label1 = new JLabel();
        label1.setBackground(SystemColor.window);
        label1.setFont(new Font(label1.getFont().getName(), Font.BOLD, 20));
        label1.setHorizontalAlignment(0);
        label1.setHorizontalTextPosition(0);
        this.$$$loadLabelText$$$(label1, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("channelForm.titleLabel"));
        panel2.add(label1, cc.xy(1, 1));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new FormLayout("fill:d:grow", "center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:d:noGrow,top:4dlu:noGrow,center:d:grow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        panel1.add(panel3, cc.xy(1, 3));
        panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), null));
        blueCheckBox = new JCheckBox();
        this.$$$loadButtonText$$$(blueCheckBox, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("channelForm.blueLabel"));
        panel3.add(blueCheckBox, cc.xy(1, 5));
        final JSeparator separator1 = new JSeparator();
        panel3.add(separator1, cc.xy(1, 7, CellConstraints.FILL, CellConstraints.TOP));
        allButton = new JButton();
        this.$$$loadButtonText$$$(allButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("channelForm.allLabel"));
        panel3.add(allButton, cc.xy(1, 9));
        redCheckBox = new JCheckBox();
        this.$$$loadButtonText$$$(redCheckBox, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("channelForm.redLabel"));
        panel3.add(redCheckBox, cc.xy(1, 1));
        greenCheckBox = new JCheckBox();
        this.$$$loadButtonText$$$(greenCheckBox, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("channelForm.greenLabel"));
        panel3.add(greenCheckBox, cc.xy(1, 3));
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return panel1;
    }
}

